home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / System / PPCReleaseDEV / Examples / Msg5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-21  |  6.8 KB  |  223 lines

  1. /* This test program sends 1000 messages to the PPC and
  2.    shows how long this takes.
  3.    The Messages are send asynchron and it`s not waited
  4.    for the reply...until all messages are send
  5.    Each Message has the Body "Text sent by M68k processor\n"
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/nodes.h>
  10. #include <exec/lists.h>
  11. #include <exec/memory.h>
  12. #include <utility/tagitem.h>
  13. #include <powerup/ppclib/interface.h>
  14. #include <powerup/ppclib/message.h>
  15. #include <powerup/ppclib/tasks.h>
  16. #include <powerup/proto/ppc.h>
  17. #include <proto/exec.h>
  18. #include <proto/dos.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "time.h"
  22. #include "time_protos.h"
  23.  
  24. #define TEXT            "Text sent by M68k processor\n"
  25. #define    MSGCOUNT    1000
  26.  
  27. struct StartupData
  28. {
  29.     ULONG    MsgCount;
  30. };
  31.  
  32. extern struct Library    *SysBase;
  33. void            *MsgArray[MSGCOUNT];
  34.  
  35. int    main(void)
  36. {
  37. struct Library        *PPCLibBase;
  38. struct TagItem        MyTags[10];
  39. void            *PPCPort;
  40. void            *ReplyPort;
  41. void            *StartupMsg;
  42. void            *M68kMsg;
  43. void            *PPCMsg;
  44. void            *ElfObject;
  45. void            *Task;
  46. UBYTE            *Body;
  47. struct StartupData    *StartupData;
  48. void            *MyTimerObject;
  49. ULONG            i;
  50.  
  51.   printf("Opening ppc.library\n");
  52.   if (PPCLibBase = OpenLibrary("ppc.library", 44))
  53.   {
  54.     if (MyTimerObject=TimerCreateObject())
  55.     {
  56.       printf("Loading PPC object\n");
  57.       if (ElfObject=PPCLoadObject("PROGDIR:Msg5PPC.elf"))
  58.       {
  59.         printf("Creating reply port\n");
  60.         MyTags[0].ti_Tag    =    TAG_DONE;
  61.         if (ReplyPort = PPCCreatePort(MyTags))
  62.         {
  63.           if (StartupMsg = PPCCreateMessage(ReplyPort, 0))
  64.           {
  65.             printf("Allocating StartupData\n");
  66.             if (StartupData = PPCAllocVec(sizeof(struct StartupData), MEMF_ANY))
  67.             {
  68.               StartupData->MsgCount    =    MSGCOUNT;
  69.  
  70.               printf("Creating PPC task\n");
  71.               MyTags[0].ti_Tag    =    PPCTASKTAG_STARTUP_MSG;
  72.               MyTags[0].ti_Data    =(ULONG) StartupMsg;
  73.  
  74.               MyTags[1].ti_Tag    =    PPCTASKTAG_STARTUP_MSGDATA;
  75.               MyTags[1].ti_Data    =(ULONG) StartupData;
  76.  
  77.               MyTags[2].ti_Tag    =    PPCTASKTAG_STARTUP_MSGLENGTH;
  78.               MyTags[2].ti_Data    =    0;
  79.  
  80.               MyTags[3].ti_Tag    =    PPCTASKTAG_STARTUP_MSGID;
  81.               MyTags[3].ti_Data    =    0;
  82.  
  83.               MyTags[4].ti_Tag    =    PPCTASKTAG_MSGPORT;
  84.               MyTags[4].ti_Data    =    TRUE;
  85.  
  86.               MyTags[5].ti_Tag    =    TAG_DONE;
  87.  
  88.               if (Task = PPCCreateTask(ElfObject, MyTags))
  89.               {
  90.                 if (PPCPort=(void*) PPCGetTaskAttrsTags(Task,
  91.                                                         PPCTASKINFOTAG_MSGPORT,0,
  92.                                                         TAG_END))
  93.                 {
  94.                   printf("Allocating memory for message body\n");
  95.                   if (Body = PPCAllocVec(sizeof(TEXT), MEMF_PUBLIC))
  96.                   {
  97.                     printf("Creating all messages...\n");
  98.                     for (i=0;i<MSGCOUNT;i++)
  99.                     {
  100.                       if ((MsgArray[i]=PPCCreateMessage(ReplyPort,
  101.                                                         sizeof(TEXT)))==NULL)
  102.                       {
  103.                         break;
  104.                       }
  105.                     }
  106.  
  107.                     if (i>=MSGCOUNT)
  108.                     {
  109.                       strcpy(Body, TEXT);
  110.                       printf("Sending 1000 ASYNCHRON messages with a *Body*...");
  111.  
  112.                       TimerSetAttr(MyTimerObject,TIMERTAG_START);
  113.                       for (i=0;i<MSGCOUNT;i++)
  114.                       {
  115.                         PPCSendMessage(PPCPort,
  116.                                        MsgArray[i],
  117.                                        Body,
  118.                                        sizeof(TEXT),
  119.                                        0x12345678);
  120.                       }
  121.                       TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  122.                       TimerShow(MyTimerObject);
  123.  
  124.                       printf("Wait for 1000 message replies...");
  125.                       TimerSetAttr(MyTimerObject,TIMERTAG_START);
  126.  
  127.                       i    =    0;
  128.  
  129.                       while (i<MSGCOUNT)
  130.                       {
  131.                         PPCWaitPort(ReplyPort);
  132.                         while (i<MSGCOUNT && PPCGetMessage(ReplyPort))
  133.                         {
  134.                           i++;
  135.                         }
  136.                       }
  137.                       TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  138.                       TimerShow(MyTimerObject);
  139.  
  140.                       printf("Waiting for Task Finish Msg...\n");
  141.                       for (;;)
  142.                       {
  143.                         if ((M68kMsg=PPCGetMessage(ReplyPort)) == StartupMsg)
  144.                         {
  145.                           printf("Ahh..the expected Startup=Finish Msg was received.\nNow we can savely free all resources.\n");
  146.                           break;
  147.                         }
  148.                         else
  149.                         {
  150.                           printf("Some non replied Msg 0x%lx was found..wait\n",
  151.                                  M68kMsg);
  152.                           PPCWaitPort(ReplyPort);
  153.                         }
  154.                       }
  155.                     }
  156.                     else
  157.                     {
  158.                       Printf("Could not create all msgs\n");
  159.                     }
  160.  
  161.                     printf("Deleting messages...\n");
  162.                     for (i=0;i<MSGCOUNT;i++)
  163.                     {
  164.                       if (MsgArray[i])
  165.                       {
  166.                         PPCDeleteMessage(MsgArray[i]);
  167.                       }
  168.                     }
  169.                   }
  170.                   else
  171.                   {
  172.                     Printf("Could not alloc mem for msg body\n");
  173.                   }
  174.                   printf("Deleting reply port...");
  175.                   while (PPCDeletePort(ReplyPort) == FALSE);
  176.                 }
  177.                 else
  178.                 {
  179.                   Printf("Could not find the PPCTask's msgport\n");
  180.                 }
  181.                 printf("Freeing message body memory\n");
  182.                 PPCFreeVec(Body);
  183.               }
  184.               else
  185.               {
  186.                 Printf("Could not create PPC task\n");
  187.               }
  188.               PPCFreeVec(StartupData);
  189.             }
  190.             else
  191.             {
  192.               Printf("Could not alloc Startup Data\n");
  193.             }
  194.             PPCDeleteMessage(StartupMsg);
  195.           }
  196.           else
  197.           {
  198.             Printf("Could not create Startup message\n");
  199.           }
  200.         }
  201.         else
  202.         {
  203.           Printf("Could not create reply port\n");
  204.         }
  205.         printf("Unloading PPC object\n");
  206.         PPCUnLoadObject(ElfObject);
  207.       }
  208.       else
  209.       {
  210.         Printf("Could not load the elfobject\n");
  211.       }
  212.       TimerDeleteObject(MyTimerObject);
  213.     }
  214.     printf("Closing ppc.library\n");
  215.     CloseLibrary(PPCLibBase);
  216.   }
  217.   else
  218.   {
  219.     Printf("Could not open ppc.library v44+\n");
  220.   }
  221. }
  222.  
  223.